﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;
namespace EasyFramework.Sys
{
    /// <summary>
    /// 注册表操作辅助类
    /// from:http://www.cnblogs.com/studyplay/archive/2011/12/09/2281790.html
    /// </summary>
    public class RegeditHelper
    {
        public RegeditHelper()
        {
        }
        //获取注册表中的数据
        public string GetRegistData(string name)
        {
            string registData;
            RegistryKey hkml = Registry.LocalMachine;
            RegistryKey software = hkml.OpenSubKey("SOFTWARE", true);
            RegistryKey aimdir = software.OpenSubKey("Microsoft", true);
            registData = aimdir.GetValue(name).ToString();
            return registData;
        }
        //写入注册表数据
        public void WTRegedit(string name, string tovalue)
        {
            RegistryKey hklm = Registry.LocalMachine;
            RegistryKey software = hklm.OpenSubKey("SOFTWARE", true);
            RegistryKey aimdir = software.CreateSubKey("XXX");
            aimdir.SetValue(name, tovalue);
        }
        //删除注册表数据
        public void DeleteRegist(string name)
        {
            string[] aimnames;
            RegistryKey hkml = Registry.LocalMachine;
            RegistryKey software = hkml.OpenSubKey("SOFTWARE", true);
            RegistryKey aimdir = software.OpenSubKey("XXX", true);
            aimnames = aimdir.GetSubKeyNames();
            foreach (string aimKey in aimnames)
            {
                if (aimKey == name)
                    aimdir.DeleteSubKeyTree(name);
            }
        }

        //判断注册表中数据是否存在
        public bool IsRegeditExit(string name)
        {
            bool _exit = false;
            string[] subkeyNames;
            RegistryKey hkml = Registry.LocalMachine;
            RegistryKey software = hkml.OpenSubKey("SOFTWARE", true);
            RegistryKey aimdir = software.OpenSubKey("Microsoft", true);
            subkeyNames = aimdir.GetSubKeyNames();
            foreach (string keyName in subkeyNames)
            {
                if (keyName == name)
                {
                    _exit = true;
                    return _exit;
                }
            }
            return _exit;
        }
    }
}